package com.programmercarl.greed;

import java.util.Arrays;
import java.util.Comparator;

/**
 * https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/description/
 * 用最少数量的箭引爆气球  参考
 *
 * @author yulongtian
 * @create 2025-02-07 14:33
 */
public class Test11 {
    public static void main(String[] args) {
        Test11 test11 = new Test11();
//        int[][] points = {{10, 16}, {2, 8}, {1, 6}, {7, 12}};
        int[][] points = {{-2147483646, -2147483645}, {2147483646, 2147483647}};
        System.out.println(test11.findMinArrowShots(points));
        System.out.println(Arrays.deepToString(points));
    }

    public int findMinArrowShots(int[][] points) {
        if (points == null || points.length == 0) {
            return 0;
        }
        Arrays.sort(points, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] == o2[0]) {
                    if (o1[1] > o2[1]) {
                        return 1;
                    } else if (o1[1] < o2[1]) {
                        return -1;
                    }
                    return 0;
                }

                if (o1[0] > o2[0]) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        int ans = 1;
        for (int i = 1; i < points.length; i++) {
            if (points[i][0] > points[i - 1][1]) {
                ans++;
            } else {
                points[i][1] = Math.min(points[i][1], points[i - 1][1]);
            }
        }
        return ans;
    }


}
